/**
 * 项目共用入口js,全局引入
 */
;(function () {
    window.API_HOST = 'http://sddpdongfei.oicp.io';
    window._fn = {
        init() {
            this.settingApplyState()
            let token = localStorage.getItem('authorization');
            if (!token) this.login()
        },
        // 选择上一个相邻兄弟类设置样式
        settingApplyState() {
            $('.slide-bar-steps').children('span.slide-bar-line').removeClass('slide-bar-line-passed');
            let currenDom = $('.slide-bar-steps').children('.slide-bar-steps-donebg');
            currenDom.prevAll('li.slide-bar-steps-done').addClass('slide-bar-steps-donebg');
            currenDom.prevAll('span.slide-bar-line').addClass('slide-bar-line-passed')
        },
        /**
         * 全局数据请求转发 统一通过此方法对外请求
         * @param url 接口请求地址后缀
         * @param data 请求数据条件
         * @param callback 请求响应 参考ajax 回调方法配置
         * @param type 请求类型 默认post
         */
        requestData(url = '', data = {}, callback = {}, type = 'post', options = {}) {
            if (!url) {
                console.log('必须填入请求地址');
                return false;
            }
            let token = localStorage.getItem('authorization');
            if (!token) this.login();
            $.ajax($.extend({}, {
                url: API_HOST + url,
                type: type,
                data: type.toUpperCase() === 'POST' ? JSON.stringify(data) : data,
                contentType: 'application/json;charset=UTF-8',
                beforeSend(XMLHttpRequest) {
                    // 把验证token放置到请求头里
                    XMLHttpRequest.setRequestHeader('authorization', token)
                    // XMLHttpRequest.abort()
                    // console.log('开始请求>>>>>')
                    if (callback.beforeSend) callback.beforeSend(XMLHttpRequest)
                },
                success(data) {
                    // console.log('请求成功>>>>>')
                    if (callback.success) callback.success(data)
                },
                error(XHR, err, obj) {
                    console.log('接口报错了>>>>', err, obj)
                    if (callback.error) callback.error(XHR, err, obj)
                }
            }, options))
        },
        login() {
            $.ajax({
                type: "POST",
                url: API_HOST + '/zczhf/login',
                data: JSON.stringify({username: '450971199610224611', password: '123456', ip: '192.168.0.1'}),
                contentType: 'application/json;charset=UTF-8',
                dataType: "json",
                success: function (data) {
                    localStorage.setItem('authorization', 'Bearer ' + data.data.authorization)
                }
            });
        },
        /**
         * 获取字典表数据
         * @param type
         * @param callback
         */
        getDist(type, callback) {
            if (type) {
                this.requestData('/zczhf/api/dict/list?type=' + type, {}, {
                    success(result) {
                        if (result.code === 0) {
                            if (callback) callback(result.data.list)
                        } else {
                            console.log(result.msg)
                        }
                    }
                })
            }
        }
    }
    window._fn.init()
})(window);